<?php

namespace App\Http\Controllers\Web;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\AdvertisementRepository;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Repository\Repositories\Interfaces\ProductCategoriesRepository;
use App\Repository\Repositories\Interfaces\ShopRepository;
use App\Repository\Repositories\Interfaces\SysArticleRepository;
use App\Repository\Repositories\Interfaces\ArticleCategoryRepository;
use App\Repository\Repositories\Interfaces\ProjectRepository;
use Illuminate\Http\Request;
use League\Flysystem\Config;
use Solarium\Client;

class HomeController extends BaseController
{

    protected $productcategories;

    protected $sysarticle;
    protected $articleCate;
    protected $advertisement;
    protected $shop;
    protected $project;

    protected $order;
    public function __construct(ProductCategoriesRepository $productcategories , SysArticleRepository $sysarticle, ShopRepository $shop, OrdersRepository $order,ArticleCategoryRepository $articleCate,AdvertisementRepository $advertisement,ProjectRepository $project)
    {
        $this->productcategories = $productcategories;
        $this->sysarticle = $sysarticle;
        $this->shop=$shop;
        $this->order=$order;
        $this->articleCate=$articleCate;
        $this->advertisement = $advertisement;
        $this->project = $project;
    }


    public function index(){

        //商品分类读取
        $categorieslist = $this->productcategories->categoriesList();

        //右侧分类新闻读取
        $new_all = [
            '0'=>['id'=>27,'name'=>'e井快报'],
            '1'=>['id'=>28,'name'=>'安防咨询'],
        ];

        foreach ($new_all as $key => $value){
            $news = $this->sysarticle->indexNewList($value['id']);
            $new_all[$key]['news']= $news;
        }

        //推荐店铺
        $shop=$this->shop->recomShop();
//        $shop=$this->shop->recommendshop();

        //商城信息读取
        $recommendcategorieslist =$this->productcategories->recommendCategoriesList();


        // 系统文章分类列表获取
        $articleCateList = $this->articleCate->getSysCateList(24);

        //首页各类广告获取
        $adv_list = $this->advertisement->indexGetAdv();

        // 获取第一页 找项目
         $project = $this->project->indexNewList(4);
        $data = [
            'categorieslist'=>$categorieslist,
            'new_all'=>$new_all,
            'recommendcategorieslist'=>$recommendcategorieslist,

            'article_cate_list'=>$articleCateList,
            'adv_list' => $adv_list,
            'shoplist'=>$shop, // 推荐店铺
            'project'=>$project, // 找项目列表

        ];
        return $this->success(ErrorConst::SUCCESS_CODE, $data);
    }

    /**
     * 商城新首页
     * @return \Illuminate\Support\Collection
     */
    public function shopindex(){

         //商品分类读取
         $categorieslist = $this->productcategories->categoriesList();

        //推荐店铺
//        $shop=$this->shop->recomShop();
        $shop=$this->shop->recommendshop();

        //推荐分类
//        $recommendcate=$this->productcategories->recommendcate();

        //最新交易
        $order=$this->order->newOrder();

         //安防市场推荐读取
         $recommendcategorieslist =$this->productcategories->recommendShopIndexCategories();

        //首页各类广告获取
        $adv_list = $this->advertisement->shopGetAdv();

         $data = [
             'categorieslist'=>$categorieslist,
             'recommendcategorieslist'=>$recommendcategorieslist,
             'shoplist'=>$shop, // 推荐店铺

//             'recommendcate'=>$recommendcate,
             'order'=>$order,
             'adv_list' => $adv_list,

         ];
         return $this->success(ErrorConst::SUCCESS_CODE, $data);
    }
    public function footerCateList(){
        // 系统文章分类列表获取
        $articleCateList = $this->articleCate->getSysCateList(24);
        
        $data = [
            'article_cate_list'=>$articleCateList
        ];
        return $this->success(ErrorConst::SUCCESS_CODE, $data);
    }

    /**
     * 首页全文搜索
     * @return \Illuminate\Support\Collection
     */
    public function searchProduct(Request $request){
        $data = $request->json()->all();

        $key=$data['key'];
        $enp_description=$data['enp_description'];
        $client =new \Solarium\Client(Config::get('database.solr'));
        $query = $client->createSelect();

//设置q   查询字符串。查询所有是*:* ， 根据指定字段查询
        $query->setQuery('all: *' . $key . "*"); //这个是设置keyword

        $result = $client->execute($query);
        $res= $result ->getResponse();
         return $this->success(ErrorConst::SUCCESS_CODE,$res->getStatusCode());
     }
    /**
     * 首页全文搜索
     * 增加索引/更新索引（根据所传id是否已经存在，存在则更新，否则新增）
     * @return \Illuminate\Support\Collection
     */
    public function updateSearchProduct(Request $request) {

            $data = $request->json()->all();

//            $solr=Config::get('database.solr');
            $options = [
                'endpoint' => [

                    'localhost' => [

                       'host' => '127.0.0.1',  //IP地址

                       'port' => 8080,         //端口号

                       'core' => null,

                       'path' => '/solr/new_core',

                       'wt'=>'json',

                    ],

               ],
            ];
            $client =new Client($options);


//            $data = array(
//                array(
//                'id' => '80922032',
//                'name' => '男士打磨直筒休闲牛仔裤',
//                'brand' => 'ENERGIE',
//                'cat' => '牛仔裤',
//                'price' => '1870.00'
//                ),
//                array(
//                'id' => '70906025',
//                'name' => '品牌LOGO翻领拉链外套',
//                'brand' => 'ENERGIE',
//                'cat' => '外套',
//                'price' => '1680.00'
//                ),
//            );
            $data=array(
                'id' => '70906025',
                'name' => '品牌LOGO翻领拉链外套',
                'cat' => '外套',
                'price' => '1680.00'
            );

            $update=$client->createUpdate($data);

            $doc=$update->createDocument();

            foreach ($data as $key=>$value){

                    $doc->$key=$value;

                }

            $update->addDocument($doc);

            $update->addCommit();

            $updateResponse=$client->update($update);

            $res = $updateResponse->getResponse();

        return $this->success(ErrorConst::SUCCESS_CODE,$res->getStatusCode());
//        return $this->success(ErrorConst::SUCCESS_CODE,$data);


    }
}
